package com.xnx3.wangmarket.plugin.robots.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.j2ee.pluginManage.controller.BasePluginController;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.AttachmentUtil;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.admin.entity.Site;
import com.xnx3.wangmarket.admin.util.SessionUtil;

/**
 * 网站管理后台
 * @author 管雷鸣
 */
@Controller
@RequestMapping("/plugin/robots/")
public class RobotsSiteAdminPluginController extends BasePluginController {
	@Resource
	private SqlService sqlService;

	/**
	 * 入口
	 */
	@RequestMapping("/index${url.suffix}")
	public String index(HttpServletRequest request, Model model) {
		if(!haveSiteAuth()){
			return error(model, "请先登录");
		}
		ActionLogUtil.insert(request, "进入设置robots.txt页面");
		return "plugin/robots/index";
	}
	

	/**
	 * edit 中，获取的原本的robots.txt的内容
	 */
	@RequestMapping(value="getRobotsTxt${url.suffix}", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO getRobotsTxt(HttpServletRequest request, Model model){
		if(!haveSiteAuth()){
			return error("请先登录");
		}
		
		Site site = SessionUtil.getSite();
		String text = AttachmentUtil.getTextByPath("site/"+site.getId()+"/robots.txt");
		if(text == null){
			text = "";
		}
		
		ActionLogUtil.insert(request, "获取robots.txt的内容");
		return success(text);
	}
	

	/**
	 * 保存 robots.txt 的内容
	 * @param txt 要保存的robots.txt 的内容
	 */
	@RequestMapping(value="save${url.suffix}", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO save(HttpServletRequest request, 
			@RequestParam(value = "txt", required = false, defaultValue="") String txt){
		if(!haveSiteAuth()){
			return error("请先登录");
		}
		
		Site site = SessionUtil.getSite();
		AttachmentUtil.putStringFile("site/"+site.getId()+"/robots.txt", txt);
		
		ActionLogUtil.insert(request, "保存robots.txt的内容");
		return success();
	}
	
}